package univer.oko.database.rmi.impl;

import univer.oko.Error;

import java.util.Vector;

/**
 * @author Eduard Dudar
 *         Date: 20.11.2005
 *         Time: 13:28:06
 */
public class EducationProgramImpl extends ImplHelper {
    public EducationProgramImpl(RMIImpl rmi) {
        super(rmi);
    }

    public int addEducationProgram(String str, Vector v) {
        if (this.isOccurs("SELECT ID_EducProg FROM Education_Program WHERE EducProg_Name = '" + str + "'")) {
            return univer.oko.Error.IN_USE;
        }

        int maxProgram = this.getIntValue("SELECT MAX(ID_EducProg) FROM Education_Program") + 1;
        int maxLP = this.getIntValue("SELECT MAX(ID_LtoP) FROM Level_by_Program") + 1;

        this.executeUpdate("INSERT INTO Education_Program VALUES(" + maxProgram + ",'" + str + "')");

        for (int i = 0; i < v.size(); i++) {
            int level = ((Integer) v.get(i)).intValue();
            this.executeUpdate("INSERT INTO Level_by_Program VALUES(" + (maxLP + i) + "," + level + "," + maxProgram + ")");
        }
        this.commit();
        return Error.ALRIGHT;
    }

    public Vector getEducationPrograms() {
        return this.getVectorValue("SELECT EducProg_Name FROM Education_Program ORDER BY EducProg_Name");
    }

    public Vector getEducationPrograms(String str) {
        return this.getVectorValue("SELECT Education_Program.EducProg_Name " +
                "FROM Education_Level, Education_Program, Level_by_Program " +
                "WHERE Education_Level.EducLevel_Name = '" + str + "' " +
                "AND Education_Level.ID_EducLevel = Level_by_Program.Ref_EducLevel " +
                "AND Level_by_Program.Ref_EducProg = Education_Program.ID_EducProg");
    }

    public int deleteEducationProgram(String str) {
        boolean isOccurs = this.isOccurs("SELECT Education_Program.EducProg_Name " +
                "FROM Education_Program, Student, Level_by_Program " +
                "WHERE Student.Ref_LtoP = Level_by_Program.ID_LtoP " +
                "AND Level_by_Program.Ref_EducProg = Education_Program.ID_EducProg " +
                "AND Education_Program.EducProg_Name = '" + str + "'");
        if (isOccurs) {
            return Error.IN_USE;
        }

        int i = this.getEducationProgramID(str);
        this.executeUpdate("DELETE FROM Level_by_Program WHERE Ref_EducProg = " + i);
        this.executeUpdate("DELETE FROM Education_Program WHERE EducProg_Name = '" + str + "'");
        this.commit();
        return Error.ALRIGHT;
    }

    public Integer getEducationProgramID(String str) {
        return this.getIntValue("SELECT ID_EducProg FROM Education_Program WHERE EducProg_Name = '" + str + "'");
    }

    public String getEducationProgramName(int id) {
        return this.getStringValue(
                "SELECT EducProg_Name FROM Education_Program, Level_by_Program " +
                        "WHERE ID_LtoP = " + id + "AND ID_EducProg = Ref_EducProg");
    }

    public Integer getLtoPID(String level, String program) {
        return this.getIntValue("SELECT Level_by_Program.ID_LtoP " +
                "FROM Education_Program, Level_by_Program, Education_Level " +
                "WHERE Education_Program.EducProg_Name = '" + program + "' " +
                "AND Education_Level.EducLevel_Name = '" + level + "' " +
                "AND Education_Program.ID_EducProg = Level_by_Program.Ref_EducProg " +
                "AND Education_Level.ID_EducLevel = Level_by_Program.Ref_EducLevel");
    }
}
